


tempfile results
foreach xxx in 15_30 30_60 60_180 180_300 300_600 600_900 ///
				30_15 60_30 180_60 300_180 600_300 900_600{
	clear
	use "cleandata\winprob_matched_dataset`xxx'"
	
	//keep if elo_percentile<=3
	
	//drop if timetaken_trim==.
	
	keep if winprob!=. & timetaken!=. & timecontrol_iv!=. & size_match==2
	
	
	reghdfe timetaken timecontrol_iv, absorb(matchvar)
	est sto firststage`xxx'
	
	ivreghdfe winprob (timetaken=timecontrol_iv), absorb(matchvar) cluster(matchvar)
	est sto iv`xxx'
	
	
		

	bys matchvar: egen timetaken_1=total(timetaken*timecontrol_iv)
	bys matchvar: egen timetaken_0=total(timetaken*!timecontrol_iv)
	gen firststage=timetaken_1-timetaken_0 if size_match==2
	
	gen marginalvalue_firststage=marginalvalueoftime*firststage 
	
	
	
//	
//	
// 	summ marginalvalue_firststage
// 	local meanmarginalvalue=r(mean)
// 	summ firststage if marginalvalueoftime!=.
// 	local meanfirststage=r(mean)


	// 	gen weight2=firststage^2 if size_match==2
// 	summ weight2
// 	local mean_weight_2=r(mean)
//	
// 	disp "Mean Weight Squared"
// 	disp `mean_weight_2'
//	
// 	summ firststage if size_match==2
// 	local mean_weight=r(mean)
//	
// 	disp "Mean Weight"
// 	disp `mean_weight'
//	
// 	summ marginalvalueoftime
// 	local unweighted_std_err=r(sd)/sqrt(r(N))
// 	local marginalvalueoftime_se=`unweighted_std_err'*sqrt(`mean_weight_2'/(`mean_weight')^2)
// 	disp "Marginal Value of Time SE:"
// 	disp `marginalvalueoftime_se'
	
	
	preserve
	parmest, fast 
	gen time="`xxx'"
	gen estimate_type="Marginal Benefit"
// 	gen marginalvalueoftime=`meanmarginalvalue'/`meanfirststage'
// 	gen marginalvalueoftime_se=`marginalvalueoftime_se'
	cap append using `results'
	save `results', replace
	restore
	
	
// 	local marginalvalueoftime= `meanmarginalvalue'/`meanfirststage'
// 	local marginalvalueoftime: di %10.3g `marginalvalueoftime'
// 	estadd local marginalcost `marginalvalueoftime'
	
	
	
	replace marginalvalue_firststage=0 if timecontrol_iv==0
	
	ivreghdfe marginalvalue_firststage (timetaken= timecontrol_iv), absorb(matchvar) cluster(matchvar)
	est sto marginalcost_iv`xxx'
	
	
	preserve
	parmest, fast 
	gen time="`xxx'"
	gen estimate_type="Marginal Cost"
	cap append using `results'
	save `results', replace
	restore
	
	
	
	gen diff_benefit_cost=winprob-marginalvalue_firststage
	ivreghdfe diff_benefit_cost (timetaken= timecontrol_iv), absorb(matchvar) cluster(matchvar)
	
	est sto diff_`xxx'
	
	if inlist("`xxx'", "15_30", "30_60", "60_180", "180_300", "300_600", "600_900"){
		
		test timetaken=0
		if _b[timetaken]>0{
			local pvalue=r(p)/2
		}
		else{
			local pvalue=1-r(p)/2
		}
		
	}
	else{
		test timetaken=0
		
		if _b[timetaken]<0{
			local pvalue=r(p)/2
		}
		else{
			local pvalue=1-r(p)/2
		}
	}
	
	estadd scalar pvalue =`pvalue': marginalcost_iv`xxx'
	
	
	preserve
	parmest, fast 
	gen time="`xxx'"
	gen estimate_type="Difference"
	cap append using `results'
	save `results', replace
	restore
	
	
	
	
}


use `results', clear
gen timecontrol=substr(time,1,strpos(time,"_")-1)
gen timecontrol_iv=substr(time, strpos(time,"_")+1,.)

destring timecontrol timecontrol_iv, replace
//
// twoway rarea min68 max68 timecontrol if timecontrol>timecontrol_iv , color(forest_green*.2) || /// 
// 	scatter estimate timecontrol if timecontrol>timecontrol_iv, color(forest_green) c(l) || ///
// 	line marginalvalueoftime timecontrol if timecontrol>timecontrol_iv, lcolor(dkorange) lpattern(dash) ///
// 	yscale(log) xscale(log) xlabel(15 30 60 180 300 600 900)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 3) label(2 "Marginal Benefit") label(3  "Marginal Cost"))  ///
// 	xtitle("Time Control") name(logscale_upper ,replace)
//	
// graph export "marginal_benefit_cost_upperbound.pdf", as(pdf) replace
//
// twoway rarea min68 max68 timecontrol if timecontrol<timecontrol_iv , color(forest_green*.2) || /// 
// 	scatter estimate timecontrol if timecontrol<timecontrol_iv, color(forest_green) c(l) || ///
// 	line marginalvalueoftime timecontrol if timecontrol<timecontrol_iv, lcolor(dkorange) lpattern(dash) ///
// 	yscale(log) xscale(log) xlabel(15 30 60 180 300 600 900)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 3) label(2 "Marginal Benefit") label(3  "Marginal Cost") ) ///
// 	xtitle("Time Control") name(logscale_lower ,replace)
//	
// graph export "marginal_benefit_cost_lowerbound.pdf", as(pdf) replace
//
//
//	
//	
// twoway rarea min68 max68 timecontrol if timecontrol>timecontrol_iv  , color(forest_green*.2) || /// 
// 	scatter estimate timecontrol if timecontrol>timecontrol_iv, color(forest_green) || ///
// 	rarea min68 max68 timecontrol if timecontrol<timecontrol_iv , color(dkorange*.2) || ///
// 	scatter estimate timecontrol if timecontrol<timecontrol_iv, color(dkorange) || ///
// 	line marginalvalueoftime timecontrol if timecontrol>timecontrol_iv, lcolor(forest_green) lpattern(dash) || ///
// 	line marginalvalueoftime timecontrol if timecontrol<timecontrol_iv, lcolor(dkorange) lpattern(dash) ///
// 	yscale(log) xscale(log) xlabel(15 30 60 180 300 600 900)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 5 4 6) label(2 " ") label(2 "Marginal Benefit (Upper Bound)") label(4 "Marginal Benefit (Lower Bound)") label(5 "Marginal Cost") label(6 "Marginal Cost")) ///
// 	xtitle("Time Control") name(logscale,replace)
//
//	
//	
	
//graph export "marginal_benefit_cost.pdf", as(pdf) replace






// gen min95=estimate-stderr*1.96
// gen max95=estimate+stderr*1.96

// gen min95_marginalvalueoftime=marginalvalueoftime-marginalvalueoftime_se*1.96
// gen max95_marginalvalueoftime=marginalvalueoftime+marginalvalueoftime_se*1.96

gen flag=.0001 if min95<.0001

replace min95=.0001 if flag!=.



replace timecontrol=timecontrol*1.01 if estimate_type=="Marginal Cost"

twoway rspike min95 max95 timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Benefit" , color(forest_green) || /// 
	scatter flag timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Benefit", color(forest_green) msym(X) || ///
	scatter estimate timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Benefit", color(forest_green) c(l) || ///
	rspike min95 max95 timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Cost" , color(dkorange) || /// 
	scatter flag timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Cost", color(dkorange) msym(X) || ///
	scatter estimate timecontrol if timecontrol>timecontrol_iv & estimate_type=="Marginal Cost", color(dkorange) c(l) lpattern(dash) msym(square)  ///
	yscale(log) xscale(log) xlabel(15 " " 30 "30 sec" 60 "1 min" 180 "3 min" 300 "5 min" 600  "10 min" 900 "15 min")  ylabel(.1 .01 .001 .0001) ///
	legend(order( 3 6) label(3 "Marginal Benefit") label(6  "Marginal Cost"))  ///
	xtitle("Time Control") name(logscale_upper ,replace)


	
graph export "exhibits/marginal_benefit_cost_upperbound.pdf", as(pdf) replace


twoway rspike min95 max95 timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Benefit" , color(forest_green) || /// 
	scatter flag timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Benefit", color(forest_green) msym(X) || ///
	scatter estimate timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Benefit", color(forest_green) c(l) || ///
	rspike min95 max95 timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Cost" , color(dkorange) || /// 
	scatter flag timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Cost", color(dkorange) msym(X) || ///
	scatter estimate timecontrol if timecontrol<timecontrol_iv & estimate_type=="Marginal Cost", color(dkorange) c(l) lpattern(dash) msym(square)  ///
	yscale(log) xscale(log)  xlabel(15 "15 sec" 30 "30 sec" 60 "1 min" 180 "3 min" 300 "5 min" 600  "10 min" 900 " ")  ylabel(.1 .01 .001 .0001) ///
	legend(order( 3 6) label(3 "Marginal Benefit") label(6  "Marginal Cost"))  ///
	xtitle("Time Control") name(logscale_lower ,replace)



graph export "exhibits/marginal_benefit_cost_lowerbound.pdf", as(pdf) replace





gen timetaken=.
//gen timecontrol_iv=.
gen winprob=.

	label var timetaken "Seconds Spent"
	label var timecontrol_iv "Time Control Indicator"
	label var winprob "Value Function"

esttab firststage15_30 firststage30_60 firststage60_180 firststage180_300 firststage300_600 firststage600_900 using "exhibits/firststage_lower.tex", drop(_cons) se label tex replace mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min") nonotes
esttab firststage30_15 firststage60_30 firststage180_60 firststage300_180 firststage600_300 firststage900_600 using "exhibits/firststage_upper.tex", drop(_cons) se label tex replace mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min") nonotes
// esttab iv15_30 iv30_60 iv60_180 iv180_300 iv300_600 iv600_900  using "iv_lower.tex", se label tex replace scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min") nonotes
// esttab iv30_15 iv60_30 iv180_60 iv300_180 iv600_300 iv900_600  using "iv_upper.tex", se label tex replace scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min") nonotes


esttab firststage15_30 firststage30_60 firststage60_180 firststage180_300 firststage300_600 firststage600_900 using "exhibits/firststage_lower_nostar.tex", drop(_cons) nostar se label tex replace mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min") nonotes
esttab firststage30_15 firststage60_30 firststage180_60 firststage300_180 firststage600_300 firststage900_600 using "exhibits/firststage_upper_nostar.tex", drop(_cons)nostar  se label tex replace mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min") nonotes
// esttab iv15_30 iv30_60 iv60_180 iv180_300 iv300_600 iv600_900  using "iv_lower_nostar.tex", nostar se label tex replace scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min") nonotes
// esttab iv30_15 iv60_30 iv180_60 iv300_180 iv600_300 iv900_600  using "iv_upper_nostar.tex", nostar se label tex replace scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min") nonotes



esttab firststage15_30 firststage30_60 firststage60_180 firststage180_300 firststage300_600 firststage600_900 , drop(_cons) se label   mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min")
esttab firststage30_15 firststage60_30 firststage180_60 firststage300_180 firststage600_300 firststage900_600 , drop(_cons) se label   mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min")
esttab iv15_30 iv30_60 iv60_180 iv180_300 iv300_600 iv600_900 , se label  scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min")
esttab iv30_15 iv60_30 iv180_60 iv300_180 iv600_300 iv900_600 , se label  scalar("widstat F-statistic" "marginalcost Marginal Cost")  mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min")





// esttab firststage15_30 firststage30_60 firststage60_180 firststage180_300 firststage300_600 firststage600_900 using "lowerbound.rtf", drop(_cons) se label rtf replace mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min") nonotes noobs collabels(none)  

esttab iv15_30 iv30_60 iv60_180 iv180_300 iv300_600 iv600_900 using "exhibits/lowerbound.tex", se label tex replace nonotes f  mtitle("15 second" "30 second" "1 min" "3 min" "5 min" "10 min")  noobs alignment(D{.}{.}{-1}) coeflabel(timetaken "Marginal Benefit")


esttab marginalcost_iv15_30 marginalcost_iv30_60 marginalcost_iv60_180 marginalcost_iv180_300 marginalcost_iv300_600 marginalcost_iv600_900 using "exhibits/lowerbound.tex", se label tex append  f nomtitles nonotes nonum coeflabel(timetaken "Marginal Cost") scalar("pvalue One-sided p-value" "widstat First stage F-statistic") alignment(D{.}{.}{-1})

esttab iv30_15 iv60_30 iv180_60 iv300_180 iv600_300 iv900_600 using "exhibits/upperbound.tex", se label tex replace nonotes    mtitle( "30 second" "1 min" "3 min" "5 min" "10 min" "15 min")  noobs f alignment(D{.}{.}{-1}) coeflabel(timetaken "Marginal Benefit")


esttab marginalcost_iv30_15 marginalcost_iv60_30 marginalcost_iv180_60 marginalcost_iv300_180 marginalcost_iv600_300 marginalcost_iv900_600  using "exhibits/upperbound.tex", se label tex append  f nomtitles nonotes nonum coeflabel(timetaken "Marginal Cost") scalar("pvalue One-sided p-value" "widstat First stage F-statistic") alignment(D{.}{.}{-1})




// twoway rarea min68 max68 timecontrol if timecontrol>timecontrol_iv , color(forest_green*.2) || /// 
// 	scatter estimate timecontrol if timecontrol>timecontrol_iv, color(forest_green) || ///
// 	rarea min68 max68 timecontrol if timecontrol<timecontrol_iv, color(dkorange*.2) || ///
// 	scatter estimate timecontrol if timecontrol<timecontrol_iv, color(dkorange) || ///
// 	line marginalvalueoftime timecontrol if timecontrol>timecontrol_iv, lcolor(maroon) || ///
// 	line marginalvalueoftime timecontrol if timecontrol<timecontrol_iv, lcolor(navy) ///
// 	 xscale(log) xlabel(30 60 180 300 600)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 4 5 6) label(2 " ") label(4 "Marginal Benefit") label(5 " ") label(6 "Marginal Cost")) yline(0) name(levels, replace)
//
//	
//	
// twoway scatter estimate timecontrol if timecontrol>timecontrol_iv, color(forest_green) c(l) || ///
// 	scatter estimate timecontrol if timecontrol<timecontrol_iv, color(dkorange) c(l) || ///
// 	line max68 timecontrol if timecontrol>timecontrol_iv, color(forest_green) lpattern(dash) || ///
// 	line min68 timecontrol if timecontrol<timecontrol_iv & min68>0 , color(dkorange) lpattern(dash) || ///
// 	line min68 timecontrol if timecontrol>timecontrol_iv & min68>0, color(forest_green) lpattern(dash) || ///
// 	line max68 timecontrol if timecontrol<timecontrol_iv, color(dkorange) lpattern(dash) || ///
// 	line marginalvalueoftime timecontrol if timecontrol>timecontrol_iv, lcolor(maroon) || ///
// 	line marginalvalueoftime timecontrol if timecontrol<timecontrol_iv, lcolor(navy) ///
// 	yscale(log) xscale(log) xlabel(30 60 180 300 600)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 4 5 6) label(2 " ") label(4 "Marginal Benefit") label(5 " ") label(6 "Marginal Cost"))  name(noci, replace)
//
//	
	
	
	
//	
//	
// twoway rarea min68 max68 timecontrol if timecontrol>timecontrol_iv , color(forest_green*.2) || /// 
// 	scatter estimate timecontrol if timecontrol>timecontrol_iv, color(forest_green) || ///
// 	rarea min68 max68 timecontrol if timecontrol<timecontrol_iv, color(dkorange*.2) || ///
// 	scatter estimate timecontrol if timecontrol<timecontrol_iv, color(dkorange) || ///
// 	line marginalvalueoftime timecontrol if timecontrol<timecontrol_iv, lcolor(navy) ///
// 	yscale(log) xscale(log) xlabel(30 60 180 300 600)  ylabel(.1 .01 .001 .0001) ///
// 	legend(order(2 4 5 6) label(2 " ") label(4 "Marginal Benefit") label(5 " ") label(6 "Marginal Cost")) name(logscale,replace)

	
